<?php

namespace App\Http\Controllers\Admin;

use App\Models\Config;
use App\Repositories\PostsMetaRepositories;
use App\Repositories\PostsRepositories;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class PostsMetaController extends BaseController
{
    public function __construct(PostsMetaRepositories $postsMetaRepositories)
    {
        $this->postsMetaRepositories = $postsMetaRepositories;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admin.config.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.config.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $ret = $this->postsMetaRepositories->save($request->all());

        if($ret)
        {
            return redirect()->route('admin.PostsMeta')->with(['success'=>'添加完成']);
        }
        return redirect()->back()->with(['success'=>'系统错误']);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $config =$this->postsMetaRepositories->find($id);
        return view('admin.config.edit',compact('config'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->only(['title','type','name','group_name','value','remark','status']);
        $ret = $this->postsMetaRepositories->update($id,$data);

        if($ret)
        {
            return redirect()->route('admin.PostsMeta')->with(['success'=>'修改成功！']);
        }
        return redirect()->back()->with(['success'=>'系统错误']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy()
    {
       $ids=\request()->input('ids');
       $ret =$this->postsMetaRepositories->delete($ids);
       if($ret) return response()->json(['ok'=>$ret]);
        return response()->json(['ok'=>222]);
    }

    public function data(Request $request)
    {
        $data = $request->all();
        //$res = $this->postsMetaRepositories->search($data);
        $res =Cache::remember(md5(http_build_query($data)),10,function () use($data){
            return $this->postsMetaRepositories->search($data);
        });

        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $res['total'],
            'data'  => $res['data']
        ];
        return response()->json($data);
    }
    public function base(Request $request)
    {
        // 获取参数
        $getGroupName = $request->input('groupName','基本');

        $groupNames = Config::query()
            ->where('status', 1)->distinct()
           ->pluck('group_name');

        //dd($groupNames->toArray());
        $PostsMeta = Cache::remember('PostsMeta',10,function (){
            return Config::query()
                ->where('status', 1)->orderBy('id')
                ->get()->groupBy('group_name')
                ->toArray();
        });


        $tab=[];

        foreach ($PostsMeta as $key => $value) {
            /*if($value['type'] == 'picture') {
                $name = $value['name'];
                $data[$name.'_cover_path'] = Helper::getPicture($value['value']);
                $data[$name.'_cover_id'] = $value['value'];
            }
            if($value['type'] == 'file') {
                $name = $value['name'];
                $data[$name.'_file_path'] = Helper::getFile($value['value']);
                $data[$name.'_file_name'] = Helper::getFile($value['value'],'name');
                $data[$name.'_file_id'] = $value['value'];
            }*/
            $tab[]=$key;
        }

        $data['groupNames'] = $groupNames;
        $data['PostsMeta'] = $PostsMeta;
        $data['tab'] = $tab;



        return view('admin.config.base',compact('data'));
    }
    public function save(Request $request)
    {

        $data =$request->all();
        unset($data['_token']);
        $ret = true;
        // 遍历插入数据
        foreach ($data as $key => $value) {
            // 修改时清空缓存
            $getResult = Config::where('name',$key)->update(['value'=>$value]);
            if($getResult === false) {
                $ret = false;
            }
        }

        if($ret)
        {
            return redirect()->route('admin.PostsMeta.base')->with(['success'=>'更新完成！']);
        }
        return redirect()->back()->with(['success'=>'系统错误，更新失败！']);
    }
}
